﻿
using System.Data;
using System.Data.SqlClient;
class Test
{

    /// <summary>
    /// 返回带数据的集合
    /// </summary>
    /// <param name="list"></param>
    /// <returns></returns>
    public DataTable GetDataTable(List<BatInfo_Model> list)
    {
        DataTable dt = new DataTable();
        dt.Columns.Add("Eqp_ID", typeof(string));
        dt.Columns.Add("PC_ID", typeof(string));
        dt.Columns.Add("OPERATION", typeof(string));
        dt.Columns.Add("IS_TRANS", typeof(double));
        dt.Columns.Add("TRAY_ID", typeof(string));
        dt.Columns.Add("CELL_ID", typeof(string));
        dt.Columns.Add("BATTERY_POS", typeof(int));
        dt.Columns.Add("MODEL_NO", typeof(string));
        dt.Columns.Add("BATCH_NO", typeof(string));
        dt.Columns.Add("TOTAL_NG_STATE", typeof(string));
        dt.Columns.Add("OCV_VOLTAGE", typeof(double));
        dt.Columns.Add("ACIR", typeof(double));
        dt.Columns.Add("TEST_NG_CODE", typeof(string));
        dt.Columns.Add("TEST_RESULT", typeof(string));
        dt.Columns.Add("TEST_RESULT_DESC", typeof(string));
        dt.Columns.Add("PostiveSHELL_VOLTAGE", typeof(double));
        dt.Columns.Add("PostiveSV_NG_CODE", typeof(string));
        dt.Columns.Add("PostiveSV_RESULT", typeof(string));
        dt.Columns.Add("PostiveSV_RESULT_DESC", typeof(string));
        dt.Columns.Add("SHELL_VOLTAGE", typeof(double));
        dt.Columns.Add("SV_NG_CODE", typeof(string));
        dt.Columns.Add("SV_RESULT", typeof(string));
        dt.Columns.Add("SV_RESULT_DESC", typeof(string));
        dt.Columns.Add("POSTIVE_TEMP", typeof(double));
        dt.Columns.Add("NEGATIVE_TEMP", typeof(double));
        dt.Columns.Add("K", typeof(double));
        dt.Columns.Add("V_DROP", typeof(double));
        dt.Columns.Add("V_DROP_RANGE", typeof(double));
        dt.Columns.Add("V_DROP_RANGE_CODE", typeof(string));
        dt.Columns.Add("V_DROP_RESULT", typeof(string));
        dt.Columns.Add("V_DROP_RESULT_DESC", typeof(string));
        dt.Columns.Add("ACIR_RANGE", typeof(double));
        dt.Columns.Add("R_RANGE_NG_CODE", typeof(string));
        dt.Columns.Add("R_RANGE_RESULT", typeof(string));
        dt.Columns.Add("R_RANGE_RESULT_DESC", typeof(string));
        dt.Columns.Add("REV_OCV", typeof(double));
        dt.Columns.Add("CAPACITY", typeof(double));
        dt.Columns.Add("END_DATE_TIME", typeof(string));
        dt.Columns.Add("TestMode", typeof(string));
        foreach (var item in list)
        {
            DataRow dataRow = dt.NewRow();
            dataRow["Eqp_ID"] = item.Eqp_ID;
            dataRow["PC_ID"] = item.PC_ID;
            dataRow["OPERATION"] = item.OPERATION;
            dataRow["IS_TRANS"] = item.IS_TRANS;
            dataRow["TRAY_ID"] = item.TRAY_ID;
            dataRow["CELL_ID"] = item.CELL_ID;
            dataRow["BATTERY_POS"] = item.BATTERY_POS;
            dataRow["MODEL_NO"] = item.MODEL_NO;
            dataRow["BATCH_NO"] = item.BATCH_NO;
            dataRow["TOTAL_NG_STATE"] = item.TOTAL_NG_STATE;
            dataRow["OCV_VOLTAGE"] = item.OCV_VOLTAGE;
            dataRow["ACIR"] = item.ACIR;
            dataRow["TEST_NG_CODE"] = item.TEST_NG_CODE;
            dataRow["TEST_RESULT"] = item.TEST_RESULT;
            dataRow["TEST_RESULT_DESC"] = item.TEST_RESULT_DESC;
            dataRow["PostiveSHELL_VOLTAGE"] = item.PostiveSHELL_VOLTAGE;
            dataRow["PostiveSV_NG_CODE"] = item.PostiveSV_NG_CODE;
            dataRow["PostiveSV_RESULT"] = item.PostiveSV_RESULT;
            dataRow["PostiveSV_RESULT_DESC"] = item.PostiveSV_RESULT_DESC;
            dataRow["SHELL_VOLTAGE"] = item.SHELL_VOLTAGE;
            dataRow["SV_NG_CODE"] = item.SV_NG_CODE;
            dataRow["SV_RESULT"] = item.SV_RESULT;
            dataRow["SV_RESULT_DESC"] = item.SV_RESULT_DESC;
            dataRow["POSTIVE_TEMP"] = item.POSTIVE_TEMP;
            dataRow["NEGATIVE_TEMP"] = item.NEGATIVE_TEMP;
            dataRow["K"] = item.K;
            dataRow["V_DROP"] = item.V_DROP;
            dataRow["V_DROP_RANGE"] = item.V_DROP_RANGE;
            dataRow["V_DROP_RANGE_CODE"] = item.V_DROP_RANGE_CODE;
            dataRow["V_DROP_RESULT"] = item.V_DROP_RESULT;
            dataRow["V_DROP_RESULT_DESC"] = item.V_DROP_RESULT_DESC;
            dataRow["ACIR_RANGE"] = item.ACIR_RANGE;
            dataRow["R_RANGE_NG_CODE"] = item.R_RANGE_NG_CODE;
            dataRow["R_RANGE_RESULT"] = item.R_RANGE_RESULT;
            dataRow["R_RANGE_RESULT_DESC"] = item.R_RANGE_RESULT_DESC;
            dataRow["REV_OCV"] = item.REV_OCV;
            dataRow["CAPACITY"] = item.CAPACITY;
            dataRow["END_DATE_TIME"] = item.END_DATE_TIME;
            dataRow["TestMode"] = item.TestMode;
            dt.Rows.Add(dataRow);
        }
        return dt;
    }

    /// <summary>
    /// 批量插入数据
    /// </summary>
    /// <param name="connectionString">连接数据库字符串</param>
    /// <param name="tableName">表名称</param>
    /// <param name="dt">需要批量插入数据库DataTable数据源</param>
    /// <param name="batchSize">一次批量插入多少条数据</param>
    /// <param name="bulkCopyTimeout">连接数据库的溢出时间</param>
    /// <returns></returns>
    public static bool SqlBulkCopyByDatatable(string connectionString, string tableName, DataTable dt, int batchSize, int bulkCopyTimeout = 120)
    {
        try
        {
            using (System.Data.SqlClient.SqlBulkCopy sqlbulkCopy = new SqlBulkCopy(connectionString, SqlBulkCopyOptions.UseInternalTransaction))
            {
                sqlbulkCopy.BatchSize = batchSize;
                sqlbulkCopy.DestinationTableName = tableName;
                sqlbulkCopy.BulkCopyTimeout = bulkCopyTimeout;
                for (int i = 0; i < dt.Columns.Count; i++)
                {
                    sqlbulkCopy.ColumnMappings.Add(dt.Columns[i].ColumnName, dt.Columns[i].ColumnName);
                }
                sqlbulkCopy.WriteToServer(dt);
                sqlbulkCopy.Close();//关闭连接
                return true;
            }
        }
        catch (System.Exception ex)
        {
            throw ex;
        }

    }


}
// <summary>
/// qt_db_ocv1:实体类(属性说明自动提取数据库字段的描述信息)
/// </summary>
[Serializable]
public partial class BatInfo_Model
{
    public BatInfo_Model()
    { }
    #region Model
    private string _eqp_id;
    private string _pc_id;
    private string _operation;
    private double? _is_trans;
    private string _tray_id;
    private string _cell_id;
    private int? _battery_pos;
    private string _model_no;
    private string _batch_no;
    private string _total_ng_state;
    private double? _ocv_voltage;
    private double? _acir;
    private string _test_ng_code;
    private string _test_result;
    private string _test_result_desc;

    private double? _postiveshell_voltage;
    private string _postivesv_ng_code;
    private string _postivesv_result;
    private string _postivesv_result_desc;

    private double? _shell_voltage;
    private string _sv_ng_code;
    private string _sv_result;
    private string _sv_result_desc;


    private double? _postive_temp;
    private double? _negative_temp;
    private double? _k;
    private double? _v_drop;
    private double? _v_drop_range;
    private string _v_drop_range_code;
    private string _v_drop_result;
    private string _v_drop_result_desc;
    private double? _acir_range;
    private string _r_range_ng_code;
    private string _r_range_result;
    private string _r_range_result_desc;
    private double? _rev_ocv;
    private double? _capacity;
    private string _end_date_time;

    private string _testMode = "自动";
    /// <summary>
    /// 
    /// </summary>
    public string Eqp_ID
    {
        set { _eqp_id = value; }
        get { return _eqp_id; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string PC_ID
    {
        set { _pc_id = value; }
        get { return _pc_id; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string OPERATION
    {
        set { _operation = value; }
        get { return _operation; }
    }
    /// <summary>
    /// 
    /// </summary>
    public double? IS_TRANS
    {
        set { _is_trans = value; }
        get { return _is_trans; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string TRAY_ID
    {
        set { _tray_id = value; }
        get { return _tray_id; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string CELL_ID
    {
        set { _cell_id = value; }
        get { return _cell_id; }
    }
    /// <summary>
    /// 
    /// </summary>
    public int? BATTERY_POS
    {
        set { _battery_pos = value; }
        get { return _battery_pos; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string MODEL_NO
    {
        set { _model_no = value; }
        get { return _model_no; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string BATCH_NO
    {
        set { _batch_no = value; }
        get { return _batch_no; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string TOTAL_NG_STATE
    {

        set { _total_ng_state = value; }
        get { return _total_ng_state; }
    }

    /// <summary>
    /// 
    /// </summary>
    public double? OCV_VOLTAGE
    {
        set { _ocv_voltage = value; }
        get { return _ocv_voltage; }
    }
    /// <summary>
    /// 
    /// </summary>
    public double? ACIR
    {
        set { _acir = value; }
        get { return _acir; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string TEST_NG_CODE
    {
        set { _test_ng_code = value; }
        get { return _test_ng_code; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string TEST_RESULT
    {
        set { _test_result = value; }
        get { return _test_result; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string TEST_RESULT_DESC
    {
        set { _test_result_desc = value; }
        get { return _test_result_desc; }
    }

    /// <summary>
    /// 
    /// </summary>
    public double? PostiveSHELL_VOLTAGE
    {
        set { _postiveshell_voltage = value; }
        get { return _postiveshell_voltage; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string PostiveSV_NG_CODE
    {
        set { _postivesv_ng_code = value; }
        get { return _postivesv_ng_code; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string PostiveSV_RESULT
    {
        set { _postivesv_result = value; }
        get { return _postivesv_result; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string PostiveSV_RESULT_DESC
    {
        set { _postivesv_result_desc = value; }
        get { return _postivesv_result_desc; }
    }

    /// <summary>
    /// 
    /// </summary>
    public double? SHELL_VOLTAGE
    {
        set { _shell_voltage = value; }
        get { return _shell_voltage; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string SV_NG_CODE
    {
        set { _sv_ng_code = value; }
        get { return _sv_ng_code; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string SV_RESULT
    {
        set { _sv_result = value; }
        get { return _sv_result; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string SV_RESULT_DESC
    {
        set { _sv_result_desc = value; }
        get { return _sv_result_desc; }
    }


    /// <summary>
    /// 
    /// </summary>
    public double? POSTIVE_TEMP
    {
        set { _postive_temp = value; }
        get { return _postive_temp; }
    }
    /// <summary>
    /// 
    /// </summary>
    public double? NEGATIVE_TEMP
    {
        set { _negative_temp = value; }
        get { return _negative_temp; }
    }
    /// <summary>
    /// 
    /// </summary>
    public double? K
    {
        set { _k = value; }
        get { return _k; }
    }
    /// <summary>
    /// 
    /// </summary>
    public double? V_DROP
    {
        set { _v_drop = value; }
        get { return _v_drop; }
    }
    /// <summary>
    /// 
    /// </summary>
    public double? V_DROP_RANGE
    {
        set { _v_drop_range = value; }
        get { return _v_drop_range; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string V_DROP_RANGE_CODE
    {
        set { _v_drop_range_code = value; }
        get { return _v_drop_range_code; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string V_DROP_RESULT
    {
        set { _v_drop_result = value; }
        get { return _v_drop_result; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string V_DROP_RESULT_DESC
    {
        set { _v_drop_result_desc = value; }
        get { return _v_drop_result_desc; }
    }
    /// <summary>
    /// 
    /// </summary>
    public double? ACIR_RANGE
    {
        set { _acir_range = value; }
        get { return _acir_range; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string R_RANGE_NG_CODE
    {
        set { _r_range_ng_code = value; }
        get { return _r_range_ng_code; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string R_RANGE_RESULT
    {
        set { _r_range_result = value; }
        get { return _r_range_result; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string R_RANGE_RESULT_DESC
    {
        set { _r_range_result_desc = value; }
        get { return _r_range_result_desc; }
    }
    /// <summary>
    /// 
    /// </summary>
    public double? REV_OCV
    {
        set { _rev_ocv = value; }
        get { return _rev_ocv; }
    }
    /// <summary>
    /// 
    /// </summary>
    public double? CAPACITY
    {
        set { _capacity = value; }
        get { return _capacity; }
    }
    /// <summary>
    /// 
    /// </summary>
    public string END_DATE_TIME
    {
        set { _end_date_time = value; }
        get { return _end_date_time; }
    }

    /// <summary>
    ///set testmode value  
    /// </summary>
    public string TestMode
    {
        set { _testMode = value; }
        get { return _testMode; }
    }
    #endregion Model

}
